<?php
class CP_SlidesController extends XPHP_Controller
{
	/**
	 * Phương thức sẽ thực thi trước khi thực thi sau khi controller được khởi tạo
	 */
	public function _init()
	{
		//Load layout
		$this->loadLayout("Administrator");
		//Xác thực
		$this->helper->auth();
	}
	
	public function indexAction()
	{
		//Lấy ra toàn bộ danh sách các slide
		$slide = new Areas_CP_Models_Slides();
		$slideList = $slide->select_all();
		//Trả về view
		$this->view->slideList = $slideList;
		
		return $this->view();
	}
	
	/**
	 * [[
	 * model: Areas_CP_Models_Slides
	 * ]]
	 */
	public function createAction()
	{
		return $this->view();
	}
	
	/**
	 * [[
	 * model: Areas_CP_Models_Slides
	 * ]]
	 */
	public function createPOST()
	{
		if($this->hasModel())
		{
			if($this->model->isValid())
			{
				//Chuyển kiểu sang kiểu int
				if($this->model->homepage == "true")
					$this->model->homepage = 1;
				else if(empty($this->model->homepage))
					$this->model->homepage = 0;
				//Insert	
				$this->model->insert();
				//Redirect
				return $this->redirect("index");
			}
		}
	}

	public function editAction($args)
	{
		$model = new Areas_CP_Models_Slides($args[0]);
		
		return $this->view($model);
	}
	
	/**
	 * [[
	 * model: Areas_CP_Models_Slides
	 * ]]
	 */
	public function editPOST()
	{
		if($this->hasModel())
		{
			if($this->model->isValid())
			{
				//Chuyển kiểu sang kiểu int
				if($this->model->homepage == "true")
					$this->model->homepage = 1;
				else if(empty($this->model->homepage))
					$this->model->homepage = 0;
				//Insert	
				$this->model->update();
				//Redirect
				return $this->redirect("index");
			}
		}
	}
	
	public function deleteAJAX()
	{
		$id = $this->params["id"];
		$model = new Areas_CP_Models_Slides($id);
		$model->delete();
		return $this->json(array("success" => true));
	}
}